<template>
  <div class='search'>
    <f-form ref='form' :action='api.hyorder' :form-key='this.formItem.orderId' :file-ids='fileIds'>
      <div>
        <f-form-item label='单位名称' placeholder='请选择单位' name='customName' @click='openCustomModal' :readOnly='true' :required='true' :value.sync='formItem.customName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择单位'>
            <a-icon type='home' style='cursor:pointer' @click='openCustomModal' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='部门' placeholder='请选择部门' name='deptName' @click='openDeptModal' :readOnly='true' :required='true' :value.sync='formItem.deptName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择部门'>
            <a-icon type='team' style='cursor:pointer' @click='openDeptModal' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='业务员' placeholder='请选择业务员' name='saleManName' @click='openSaleManModal' :readOnly='true' :required='true' :value.sync='formItem.saleManName' :trigger-change='true' v-if='formItem.deptId'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择业务员'>
            <a-icon type='user' style='cursor:pointer' @click='openSaleManModal' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='发货方式' extra='切换发货方式前需清空收款信息' type='radio' name='sendType' :required='true' dataType='number' :data='api.busCommon.sendType' :value.sync='formItem.sendType' :disabled='formItem.bankLists[0].bankId' />
        <f-form-item label='资保金方式' type='radio' name='customBondType' :required='true' dataType='number' :data='api.busCommon.customBondType' :value.sync='formItem.customBondType' v-if='formItem.sendType==3' />
        <f-form-item label='资信额度' type='number' name='reputationAmount' dataType='number' :value='reputationAmount' :precision='2' disabled v-if='formItem.sendType==4' />

        <!--现款,资保金,预收款-->
        <f-form-item type='other' style='width:100%' label='收款金额' v-if='formItem.sendType!=4'>
          <template v-for='(item,index) in formItem.bankLists'>
            <a-select placeholder='收款方式' :default-value='1' style='width: 140px;' v-model='item.amountType' @change='(value)=>amountTypeChange(item,value)' :disabled='item.isEdit'>
              <a-select-option :value='1' v-if='formItem.sendType!=2'>
                实时收款
              </a-select-option>
              <a-select-option :value='2' v-if='formItem.saleManAmount>0 && formItem.sendType==3'>
                账户余额
              </a-select-option>
              <a-select-option :value='3'>
                认款记录
              </a-select-option>
            </a-select>
            <a-input-number placeholder='收款金额' name='takeAmount' disabled style='width:140px;margin-left: 5px' v-model='item.takeAmount' v-if='item.amountType!=2'></a-input-number>
            <a-select placeholder='收款银行' disabled style='width: 160px;margin-left: 5px' name='bankId' v-model='item.bankId' v-if='item.amountType!=2'>
              <a-select-option :value='item.value' v-for='(item,i) in bankList' :key='i'>{{ item.label }}</a-select-option>
            </a-select>
            <a-date-picker placeholder='收款日期' disabled name='takeDate' style='width: 140px;margin-left: 5px' v-model='item.takeDate' v-if='item.amountType!=2'></a-date-picker>
            <a-input-number placeholder='可抵扣金额' name='takeAmount' disabled style='width:170px;margin-left: 5px' :defaultValue='formItem.saleManAmount' v-model='formItem.saleManAmount' v-if='item.amountType==2 && formItem.sendType==3'></a-input-number>
            <!--选择流水-->
            <a-button type='dashed' icon='account-book' @click='openBankStatementModal(item)' style='width:110px;margin-left: 5px' v-if='item.amountType==1 && !item.isEdit'>
              选择流水
            </a-button>
            <!--选择现款收款-->
            <a-button type='dashed' icon='account-book' @click='openCustomXkBalanceModal(item)' style='width:110px;margin-left: 5px' v-if='!item.isEdit && formItem.sendType==1 && item.amountType==3'>
              选择认款
            </a-button>
            <!--选择资保金认款-->
            <a-button type='dashed' icon='account-book' @click='openBondBalanceModal(item)' style='width:110px;margin-left: 5px' v-if='!item.isEdit && formItem.sendType==3 && item.amountType==3'>
              选择认款
            </a-button>
            <!--预收款-->
            <a-button type='dashed' icon='account-book' @click='openCustomXkBalanceModal(item)' style='width:110px;margin-left: 5px' v-if='!item.isEdit && formItem.sendType==2 && item.amountType==3'>
              选择认款
            </a-button>

            <a-icon type='plus-circle' @click='() => addBank()' style='font-size: 18px;margin-left: 5px' v-if='item.bankId' />
            <a-icon type='minus-circle-o' @click='() => removeBank(index,item)' style='font-size: 18px;margin-left: 5px' />
            <br>
          </template>
        </f-form-item>

        <f-form-item label='发货信息' type='other'>
          <f-table ref='orderDrugs' rowKey='orderDetailsId' :tableColumns='orderDetailsColumns' :scroll='{x:true}' @onSuccess='onLoad' :operator='operator' :params='queryParam' :dataSource='drugList' :autoLoad='false' :showPagination='false' :multiple='false'>
            <!--存在审批流程的订单不允许修改商品,如需修改订单商品需要申请撤销审批流程-->
            <span slot='action' slot-scope='text, record,index' v-if='!formItem.processInstanceId || formItem.orderState==1'>
             <template v-if='record.rowIndex!="合计"'>
               <a @click='editDrug(record)'>编辑</a>
               <!--<a-divider type='vertical' />
               <a v-if='record.saleType==1'>票折</a>-->
               <a-divider type='vertical' v-if='!record.orderDetailsId' />
               <a href='javascript:;' @click='removeDrug(record,index)' v-if='!record.orderDetailsId'>移除</a>
               <a-divider type='vertical' />
               <a href='javascript:;' @click='delDrug(record,index)' v-if='record.orderDetailsId'>删除</a>
             </template>
            </span>
            <span slot='footer' slot-scope='currentPageData'>
                <template>
              <span style='padding-left:10px;color: red;font-weight: bold'>合计销售金额:<span style='padding-left: 8px'>{{ totalSaleAmount }}</span></span>
            </template>
            </span>
          </f-table>
        </f-form-item>
        <f-form-item label='收货地址' placeholder='请选择收货地址' name='customAddress' @click='openCustomAddressModal' :readOnly='true' :required='true' :value.sync='formItem.customAddress' :trigger-change='true' v-show='formItem.customCode'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择'>
            <a-icon type='money-collect' style='cursor:pointer' @click='openCustomAddressModal' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='收货人' name='customUserName' :value='formItem.customUserName' />
        <f-form-item label='收货人电话' name='customUserTel' :value='formItem.customUserTel' />
        <f-form-item label='是否开票' name='invoiceState' type='radio' dataType='number' :required='true' :data='api.busCommon.invoiceState' :value.sync='formItem.invoiceState' />
        <f-form-item label='发票方式' name='invoiceSendType' type='radio' dataType='number' :data='api.busCommon.invoiceSendType' :value.sync='formItem.invoiceSendType' />
        <f-form-item label='发票类型' name='invoiceType' type='radio' dataType='number' :data='api.busCommon.invoiceType' :value.sync='formItem.invoiceType' />
        <f-form-item label='发票要求' name='invoiceRemark' type='other' >
          <a-input-group compact>
            <a-input style='width: 30%' v-model='formItem.invoiceRemark' />
            <a-select style='width: 25%' @change='invoiceRemarkChange' placeholder='常用语选择'>
              <a-select-option value='发票备注批号和效期，生产日期'>
                发票备注批号和效期，生产日期
              </a-select-option>
              <a-select-option value='发票备注批号和效期'>
                发票备注批号和效期
              </a-select-option>
              <a-select-option value='发票备注批号'>
                发票备注批号
              </a-select-option>
              <a-select-option value='发票复印件随货'>
                发票复印件随货
              </a-select-option>
            </a-select>
          </a-input-group>
        </f-form-item>
        <f-form-item label='随货资料' name='sendRemark' type='other'>
          <a-input-group compact>
            <a-input style='width: 30%' v-model='formItem.sendRemark' />
            <a-select style='width: 25%' @change='sendRemarkChange' placeholder='常用语选择'>
              <a-select-option value='厂检、出库单'>
                厂检、出库单
              </a-select-option>
              <a-select-option value='发票'>
                发票
              </a-select-option>
              <a-select-option value='随货同行单'>
                随货同行单
              </a-select-option>
              <a-select-option value='购销合同'>
                购销合同
              </a-select-option>
            </a-select>
          </a-input-group>
        </f-form-item>
        <f-form-item label='审批备注' extra='该备注仅用于结算系统审单' name='applyRemark' :value='formItem.applyRemark' />
        <f-form-item label='备注' extra='该备注同步到海鹰备注栏' name='remark' :value='formItem.remark' />
        <f-form-item label='父订单' name='parentId' :value='formItem.parentId' />
        <f-form-item label='附件' type='upload' name='orderImage' :value='formItem.orderImage' :file-max='20' :disabled='readOnly' :busId='this.formItem.orderId' busType='HyOrder' :realRemove='true' @uploadSuccess='uploadSuccess' />
      </div>
    </f-form>
    <!--客户选择-->
    <select-hy-remoter-custom-modal ref='customModal' @onSelectChange='onSelectCustomChange'></select-hy-remoter-custom-modal>
    <!--添加商品弹窗-->
    <order-drug-modal ref='orderDrugModal' @onSubmit='onAddDrugSubmit'></order-drug-modal>
    <!--业务员选择-->
    <select-sale-man-modal ref='saleManModal' @onSelectChange='onSelectSaleManChange' :selectType='2'></select-sale-man-modal>
    <!--部门地址-->
    <select-dept-modal ref='deptModal' @onSelectChange='onSelectDeptChange'></select-dept-modal>
    <!--收货地址-->
    <select-custom-address-modal ref='customAddressModal' @onSelectChange='onSelectCustomAddressChange'></select-custom-address-modal>
    <!--预收款-->
    <select-custom-balance-modal ref='customBalanceModal' @onSelectChange='onSelectCustomBalanceChange'></select-custom-balance-modal>
    <!--现款认款-->
    <select-xk-balance-modal ref='xkBalanceModal' @onSelectChange='onSelectXkBalanceChange' :multiple='false'></select-xk-balance-modal>
    <!--资保金认款-->
    <select-bond-balance-modal ref='bondBalanceModal' @onSelectChange='onSelectBondBalanceChange' :multiple='false'></select-bond-balance-modal>
    <!--流水选择-->
    <select-bank-statement-modal ref='bankStatementModal' @onSelectChange='onSelectBankStatementModalChange'></select-bank-statement-modal>
  </div>
</template>
<script>
import api from "../js/api"
import SelectHyRemoterCustomModal from "../components/select_hy_remoter_custom_modal"
import CustomAddressModal from "../../business/custom/custom_address_modal"
import OrderDrugModal from "./order_drug_modal"
import SelectSaleManModal from "../components/select_saleman_modal"
import SelectDeptModal from "../components/select_dept_modal"
import SelectCustomAddressModal from "@/views/module/haiying/components/select_custom_address_modal"
import SelectCustomBalanceModal from "@/views/module/haiying/components/select_custom_balance_modal"
import SelectXkBalanceModal from "@/views/module/haiying/components/select_xk_balance_modal"
import SelectBondBalanceModal from "@/views/module/haiying/components/select_bond_balance_modal"
import { commonMixins } from "@/core/mixins/commonMixins"
import SelectBankStatementModal from "@/views/module/haiying/components/select_bank_statement_modal"

export default {
  name: "orderForm",
  props: {
    processInstanceId: {
      type: String,
      default: ""
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  mixins: [commonMixins],
  components: { SelectCustomBalanceModal, SelectCustomAddressModal, SelectSaleManModal, SelectBankStatementModal, SelectBondBalanceModal, SelectHyRemoterCustomModal, CustomAddressModal, OrderDrugModal, SelectDeptModal, SelectXkBalanceModal },
  data() {
    //操作按钮
    let operator = []

    operator.push({
      name: "添加商品",
      icon: "plus",
      that: this,
      handler: (table) => {
        this.$refs["orderDrugModal"].open(this.formItem)
      }
    })

    return {
      refs: this.$refs,
      addressType: 1,
      bankList: [],
      //列表初始化查询参数
      queryParam: {},
      params1: {
        customCode: "",
        addressType: 1
      },
      autoLoad: false,
      //现款认款记录
      currentItem: {},
      //流水记录
      bankStatementItem: {},
      //当前资保金认款记录
      currentBondItem: {},
      //预收款记录
      yskCurrentItem: {},
      //流水记录
      //业务员资信额度
      reputationAmount: 0,
      //销售金额
      totalSaleAmount: 0,
      //票折金额
      totalDiscountAmount: 0,
      readOnly: false,
      amountTitle: "回款日期",
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      loaded: false,//是否加载完成
      operator: operator,
      removeIds: [],
      fileIds: [],
      drugList: [],
      orderDetailsColumns: [],
      formItem: this.getFormItem()
    }
  },
  watch: {
    "formItem.customBondType"(newVal) {
      if (newVal != 0) {

      }
    },
    "formItem.sendType"(value, old) {
      if (this.isAdd) {
        this.formItem.bankLists = [{ amountType: undefined, bankId: undefined, takeDate: null, takeAmount: null }]
      }
      this.$forceUpdate()
    },
    "drugList"(data) {
      this.computeSaleAmount(data)
    }
  },
  methods: {
    getFormItem() {
      return {
        orderId: "",
        orderNo: "",
        delivery: "",
        saleManName: "",
        customName: "",
        companyId: null,
        invoiceSendType: 2,
        invoiceType: 2,
        invoiceState: null,
        reputation: 0,
        areaIds: [],
        deptType: "ZY",
        reputationAmount: 0,
        customShId: null,
        //业务员账户资保金余额
        saleManAmount: 0,
        bankId: null,
        sendType: 1,
        customBondType: null,
        supplierBondType: 0,
        customCode: "",
        orderImage: [],
        sendRemark: "厂检、出库单",
        bankLists: [{ amountType: undefined, bankId: undefined, takeDate: null, takeAmount: null }],
        billsType: "",
        sendDate: this.$moment().format("YYYY-MM-DD"),
        customAddress: "",
        customAddressId: null,
        remark: ""
      }
    },
    getOrderDetailsColumns() {
      let column = [{
        title: "序号",
        width: "60px",
        align: "left",
        fixed: "left",
        dataIndex: "rowIndex",
        customRender: function(text, record, index) {
          return (record.rowIndex !== "合计") ? (parseInt(index) + 1) : record.rowIndex
        }
      }, {
        title: "商品名称",
        align: "center",
        ellipsis: true,
        width: "140px",
        dataIndex: "drugsName"
      }, {
        title: "规格",
        align: "center",
        ellipsis: true,
        width: "140px",
        dataIndex: "drugsSpec"
      }, {
        title: "仓库",
        align: "center",
        width: "120px",
        ellipsis: true,
        dataIndex: "storeName"
      }, {
        title: "批号",
        align: "center",
        width: "90px",
        ellipsis: true,
        dataIndex: "drugLotNo"
      }, {
        title: "数量",
        align: "center",
        width: "60px",
        needTotal: true,
        dataIndex: "saleNum"
      }, {
        title: "单价",
        align: "center",
        width: "60px",
        dataIndex: "salePrice"
      }, {
        title: "金额",
        align: "center",
        width: "80px",
        needTotal: true,
        dataIndex: "saleAmount"
      }, {
        title: "件容",
        align: "center",
        width: "60px",
        dataIndex: "drugJr"
      }, {
        title: "有效期至",
        align: "center",
        width: "100px",
        dataIndex: "expirationDate"
      }, {
        title: "生产企业",
        align: "center",
        width: "130px",
        ellipsis: true,
        dataIndex: "producersName"
      }, {
        title: "操作",
        align: "center",
        dataIndex: "action",
        width: "90px",
        fixed: "right",
        scopedSlots: { customRender: "action" }
      }]
      return column
    },
    //表单赋值
    setData(data) {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.orderDetailsColumns = this.getOrderDetailsColumns()
      if (data && data.orderId) {
        let params = {
          orderId: data.orderId
        }
        //查询订单明细
        this.refs["form"].getDetails(params, (data) => {
          this.formItem = data
          //this.formItem.areaIds = [this.formItem.areaId]
          this.formItem.deptType = data.deptType
          //资信额度
          this.reputationAmount = data.reputationAmount || 0
          //资保金余额
          this.formItem.saleManAmount = data.amount || 0
          data.drugList.forEach((item) => {
            this.drugList.push(item)
          })
          let bankLists = data.bankLists
          if (bankLists) {
            this.formItem.bankLists = JSON.parse(bankLists)
            if (this.formItem.bankLists.length == 0) {
              this.formItem.bankLists = [{ amountType: undefined, bankId: undefined, takeDate: null, takeAmount: null }]
            }
          }
          this.$refs["orderDrugs"].refresh(false)
          this.loaded = true
        })
        this.isAdd = false
        this.loaded = true
      } else {
        let localData = localStorage.getItem("orderFormJson")
        if (localData) {
          this.formItem = JSON.parse(localData)
          let drugData = JSON.parse(this.formItem.drugList)
          let bankLists = this.formItem.bankLists
          if (bankLists) {
            let bankData = JSON.parse(this.formItem.bankLists)
            this.formItem.bankLists = bankData
          }
          drugData.forEach((item) => {
            this.drugList.push(item)
          })

          this.$forceUpdate()
          this.$refs["orderDrugs"].refresh(false)
        }
        this.formItem.areaIds = []
        this.formItem.parentId = data.parentId
        this.loaded = true
      }

    },

    getBankList() {
      this.http.get(this, {
        url: this.api.bank.list,
        params: {},
        success: function(me, data) {
          data.map(item => {
            item.label = item.bankName
            item.value = item.bankId
          })
          me.bankList = data
        }
      })
    },
    amountTypeChange(item, value) {
      item.paymentId = null
      item.paymentOrderId = null
      item.takeDate = null
      item.bankId = undefined
      item.takeAmount = null
      this.$forceUpdate()
    },
    /**
     * 删除收款
     * @param index
     */
    removeBank(index, item) {
      let amountType = item.amountType
      if (!this.isAdd) {
        if (item.paymentOrderId) {
          this.http.confirm(this, {
            content: "确认删除吗?",
            url: api.paymentOrder.remove,
            data: {
              paymentOrderId: item.paymentOrderId
            },
            success: (me, data) => {
              this.removeBankItem(index)
            }
          })
        } else {
          this.removeBankItem(index)
        }
      } else {
        this.removeBankItem(index)
      }
    },

    removeBankItem(index) {
      this.formItem.bankLists.splice(index, 1)
      if (this.formItem.bankLists.length == 0) {
        this.formItem.bankLists = [{ amountType: undefined, bankId: undefined, takeDate: null, takeAmount: null }]
      }
      this.$forceUpdate()
    },
    /**
     * 添加收款
     */
    addBank() {
      this.formItem.bankLists.push({ amountType: undefined, bankId: undefined, takeAmount: null, tankDate: null, paymentOrderId: null })
      this.$forceUpdate()
    },

    /**
     * 删除预付款
     * @param index
     */
    removeYfk(index, item) {
      if (!this.isAdd) {
        this.http.post(this, {
          url: api.payment.unBindOrder,
          data: {
            paymentId: item.paymentId
          },
          success: (me, data) => {
            this.formItem.bankLists.splice(index, 1)
            this.$forceUpdate()
          }
        })
      } else {
        this.formItem.bankLists.splice(index, 1)
      }
      this.$forceUpdate()
    },
    /**
     * 商品加载完成
     */
    onLoad(record) {
      if (record.total > 0) {

      }
    },
    /**
     * 编辑商品
     */
    editDrug(record) {
      this.$refs["orderDrugModal"].open(this.formItem, record)
    },
    /**
     * 移除商品
     */
    removeDrug(record, index) {
      this.drugList.splice(index, 1)
    },
    /**
     * 删除商品
     */
    delDrug(record, index) {
      this.http.confirm(this, {
        content: "确认删除吗?",
        url: api.hyOrderDetails.deleteById,
        data: {
          orderDetailsId: record.orderDetailsId
        },
        success: (me, data) => {
          this.drugList.splice(index, 1)
        }
      })
    },

    /**
     * 添加商品回调
     */
    onAddDrugSubmit(drug, isAdd) {
      if (isAdd) {
        this.drugList.push(drug)
      }
      this.computeSaleAmount(this.drugList)
      this.handleSuccess()
    },
    /**
     * 选择业务员
     */
    openSaleManModal() {
      this.$refs["saleManModal"].open({
        deptId: this.formItem.deptId,
        customCode: this.formItem.customCode,
        selectType: 2
      })
    },
    /**
     * 业务员选择回调
     */
    onSelectSaleManChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "saleManName": selectedRows[0].saleManName })
      this.formItem.saleManName = selectedRows[0].saleManName
      this.formItem.saleManId = selectedRowKeys[0]
      this.formItem.saleManAmount = selectedRows[0].amount
      this.reputationAmount = selectedRows[0].reputationAmount
      if (selectedRows[0].customUserName) {
        this.formItem.customUserName = selectedRows[0].customUserName
        this.formItem.customUserTel = selectedRows[0].customUserTel
      }
      this.$forceUpdate()
    },
    /**
     * 部门选择回调
     */
    onSelectDeptChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "deptName": selectedRows[0].deptName })
      this.formItem.deptName = selectedRows[0].deptName
      this.formItem.deptId = selectedRows[0].deptId
      this.formItem.hyDeptNo = selectedRows[0].hyDeptNo
      this.formItem.deptType = selectedRows[0].deptType
      this.$refs.form.getForm().setFieldsValue({ "saleManName": "" })
      this.formItem.saleManName = null
      this.formItem.saleManId = null
      this.formItem.saleManAmount = 0
      this.reputationAmount = 0
      this.$forceUpdate()
    },
    /**
     * 银行选择回调
     */
    onSelectBankChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "bankName": selectedRows[0].bankName + "(" + selectedRows[0].bankNo + ")" })
      this.formItem.bankName = selectedRows[0].bankName
      this.formItem.bankId = selectedRowKeys[0]
      this.$forceUpdate()
    },
    /**
     * 选择收货地址回调
     */
    onSelectCustomAddressChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "customAddress": selectedRows[0].address })
      this.formItem.customAddress = selectedRows[0].address
      this.formItem.customAddressId = selectedRowKeys[0]
      this.$forceUpdate()
    },

    /**
     * 购货方选择回调
     */
    onSelectCustomChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "customName": selectedRows[0].customName })
      this.$refs.form.getForm().setFieldsValue({ "customAddress": selectedRows[0].ckAddress })
      this.formItem.customName = selectedRows[0].customName
      this.formItem.sendAreaId = selectedRows[0].sendAreaId
      this.formItem.sendArea = selectedRows[0].sendArea
      this.formItem.customCode = selectedRowKeys[0]
      this.formItem.customAddress = selectedRows[0].ckAddress
      this.formItem.customUserName = selectedRows[0].linkMan
      this.formItem.customUserTel = selectedRows[0].linkTel
      this.formItem.customAddressId = 0
      this.$forceUpdate()
    },
    /**
     * 打开银行流水
     */
    openBankStatementModal(item) {
      this.bankStatementItem = item
      this.$refs["bankStatementModal"].open({
        customCode: this.formItem.customCode
      })
    },
    /**
     * 银行流水选择回调
     */
    onSelectBankStatementModalChange(selectedRowKeys, selectedRows) {
      let hasObj = this.formItem.bankLists.find(i => i.bankStatementId == selectedRows[0].bankStatementId)
      if (hasObj) {
        this.$message.error("请勿重复选择")
        return
      }
      this.bankStatementItem.amountType = 1
      this.bankStatementItem.takeDate = selectedRows[0].amountDate
      this.bankStatementItem.takeAmount = selectedRows[0].amount
      this.bankStatementItem.bankId = selectedRows[0].takeBankId
      this.bankStatementItem.bankStatementId = selectedRows[0].bankStatementId
      let customId = selectedRows[0].customId
      if (customId) {
        //this.formItem.customName = selectedRows[0].customName
        // this.formItem.customId = selectedRows[0].customId
        // this.formItem.customCode = selectedRows[0].customCode
        // this.$refs.form.getForm().setFieldsValue({ "customName": selectedRows[0].customName })
      }
      this.$forceUpdate()
    },
    /**
     * 文件上传回调
     */
    uploadSuccess(fileUrl, files, fileIds, removeIds) {
      this.fileIds = fileIds
      this.removeIds.push(...removeIds)
    },
    discount(record) {
      this.formItem = record
      this.discountModal = true
    },
    discountSubmit() {
      this.http.post(this, {
        url: this.api.bod.discount,
        data: {
          orderDetailsId: this.formItem.orderDetailsId,
          saleAmount: this.formItem.saleAmount
        },
        success(me, data) {
          me.$refs["orderDrugs"].refresh(false)
          me.discountModal = false
        }
      })
    },
    //客户预付款货款余额
    openCustomBalanceModal(item) {
      this.yskCurrentItem = item
      this.$refs["customBalanceModal"].open({
        customCode: this.formItem.customCode
      })
    },
    //客户全款收款记录
    openCustomXkBalanceModal(item) {
      this.currentItem = item
      this.$refs["xkBalanceModal"].open({
        customCode: this.formItem.customCode
      })
    },
    //资保金收款记录
    openBondBalanceModal(item) {
      this.currentBondItem = item
      this.$refs["bondBalanceModal"].open({
        saleManId: this.formItem.saleManId
      })
    },
    /**
     * 预收款余额记录择回调
     */
    onSelectCustomBalanceChange(selectedRowKeys, selectedRows) {
      let hasObj = this.formItem.bankLists.find(i => i.paymentId == selectedRows[0].paymentId)
      if (hasObj) {
        this.$message.error("请勿重复选择")
        return
      }
      this.yskCurrentItem.amountType = 3
      let rows = selectedRows[0]
      this.yskCurrentItem.paymentId = rows.paymentId
      this.yskCurrentItem.bankStatementId = rows.bankStatementId
      this.yskCurrentItem.bankId = rows.bankId
      this.yskCurrentItem.takeAmount = rows.surplusAmount
      this.yskCurrentItem.takeDate = rows.amountDate
      this.$forceUpdate()
    },

    /**
     * 现款记录择回调
     */
    onSelectXkBalanceChange(selectedRowKeys, selectedRows) {
      let hasObj = this.formItem.bankLists.find(i => i.paymentId == selectedRows[0].paymentId)
      if (hasObj) {
        this.$message.error("请勿重复选择")
        return
      }
      this.currentItem.amountType = 3
      let rows = selectedRows[0]
      this.currentItem.paymentId = rows.paymentId
      this.currentItem.bankStatementId = rows.bankStatementId
      this.currentItem.bankId = rows.bankId
      this.currentItem.takeAmount = rows.surplusAmount
      this.currentItem.takeDate = rows.amountDate
      this.$forceUpdate()
    },

    /**
     * 资保金记录择回调
     */
    onSelectBondBalanceChange(selectedRowKeys, selectedRows) {
      let hasObj = this.formItem.bankLists.find(i => i.paymentId == selectedRows[0].paymentId)
      if (hasObj) {
        return
      }
      this.currentBondItem.amountType = 3
      let rows = selectedRows[0]
      this.currentBondItem.paymentId = rows.paymentId
      this.currentBondItem.bankId = rows.bankId
      this.currentBondItem.bankStatementId = rows.bankStatementId
      this.currentBondItem.takeAmount = rows.amount
      this.currentBondItem.takeDate = rows.amountDate
      this.$forceUpdate()
    },

    invoiceRemarkChange(value) {
      this.formItem.invoiceRemark = value
      this.$forceUpdate()
    },
    sendRemarkChange(value) {
      this.formItem.sendRemark = value
      this.$forceUpdate()
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.$forceUpdate()
    },
    checkBankItem(bankItem, sendType) {
      let tag = true
      bankItem.forEach((item) => {
        let amountType = item.amountType
        if ((amountType == 1 || amountType == 3) && sendType != 4) {
          if (item.bankId == undefined || !item.takeAmount || !item.takeDate) {
            tag = false
          }
        }
      })
      return tag
    },
    /**
     * 提交表单
     */
    handleSubmit(callback, beforeSubmit) {
      /* if (this.multipleArea == 1 || this.formItem.deptType == "ZS") {
         if (!this.formItem.areaIds[0]) {
           this.$message.error("请选择业务区域")
           return
         }
       }*/
      let bankLists = this.formItem.bankLists
      let sendType = this.formItem.sendType
      if (bankLists && bankLists.length > 0) {
        let tag = this.checkBankItem(bankLists, sendType)
        if (tag == false) {
          this.$message.error("请填写收款信息")
          return
        }
      }
      //提交表单(表单中没有的字段需要单独声明提交)
      let drugs = []
      this.drugList.forEach((item) => {
        drugs.push(item)
      })
      this.$refs.form.submit({
        drugList: JSON.stringify(drugs),
        bankLists: JSON.stringify(this.formItem.bankLists),
        customCode: this.formItem.customCode,
        companyId: this.formItem.companyId,
        deptId: this.formItem.deptId,
        customId: this.formItem.customId,
        customAddressId: this.formItem.customAddressId,
        customAddress: this.formItem.customAddress,
        customUserName: this.formItem.customUserName,
        customUserTel: this.formItem.customUserTel,
        customBalanceId: this.formItem.customBalanceId,
        invoiceRemark: this.formItem.invoiceRemark,
        bankId: this.formItem.bankId,
        hyDeptNo: this.formItem.hyDeptNo,
        saleManId: this.formItem.saleManId,
        sendType: this.formItem.sendType,
        hostName: this.formItem.hostName,
        totalSaleAmount: this.totalSaleAmount,
        totalDiscountAmount: this.totalDiscountAmount,
        //proId: this.formItem.areaIds[0],
        sendAreaId: this.formItem.sendAreaId,
        sendArea: this.formItem.sendArea,
        deptType: this.formItem.deptType,
        userId: this.formItem.userId,
        parentId: this.formItem.parentId,
        processInstanceId: this.formItem.processInstanceId,
        historyOrder: 0
      }, callback, () => {
        beforeSubmit && beforeSubmit()
      }, "orderFormJson")
    },
    computeSaleAmount(data) {
      let totalSaleAmount = 0
      let totalDiscountAmount = 0
      data.forEach((item) => {
        if (item.drugsName) {
          totalSaleAmount += Number(item.saleAmount)
          if (item.discountPrice && item.discountPrice > 0) {
            totalDiscountAmount += Number(item.discountPrice)
          }
        }
      })
      this.totalSaleAmount = parseFloat(totalSaleAmount).toFixed(2)
      this.totalDiscountAmount = parseFloat(totalDiscountAmount).toFixed(2)
    },
    handleSuccess() {
      this.$refs["orderDrugs"].refresh()
    }
  },
  mounted() {

  },
  created() {
    this.getBankList()
  }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>
/deep/ .ant-table-footer {
  padding: 6px 16px;
}
</style>
